<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\CommissionRecords
 *
 * @property int $id
 * @property int $uid
 * @property float $amount
 * @property int $trade_type
 * @property string $body
 * @property int $created_at
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereBody($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereTradeType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereUid($value)
 * @mixin \Eloquent
 * @property int $order_id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereOrderId($value)
 * @property string|null $subject
 * @property int $type
 * @property int $buyer_uid
 * @property string|null $buyer_name
 * @property-read \App\Models\User $buyer
 * @property-read \App\Models\Order $order
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereBuyerName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereBuyerUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereSubject($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CommissionRecords whereType($value)
 */
class CommissionRecords extends Model
{
    protected $table = 'commission_records';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function ($model){
            if (!$model->created_at) $model->created_at = time();
        });
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    public function buyer()
    {
        return $this->hasOne(User::class, 'uid', 'buyer_uid');
    }

    public function order()
    {
        return $this->hasOne(Order::class, 'order_id', 'order_id');
    }
}
